package net.expectx.pay.controller.system;

import io.swagger.annotations.ApiOperation;

import net.expectx.common.single.base.BaseResult;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.AdminResult;
import net.expectx.pay.common.BaseController;
import net.expectx.pay.common.util.ExpectXUtil;
import net.expectx.pay.dao.bean.SystemTypeExample;
import net.expectx.pay.dao.bean.SystemTypeGroup;
import net.expectx.pay.dao.bean.SystemTypeGroupExample;
import net.expectx.pay.rpc.api.SystemTypeGroupService;
import net.expectx.pay.rpc.api.SystemTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2018/09/06
 *
 * @author Rui
 */
@Controller
@RequestMapping(value = "/manager/system/systemTypeGroup")
public class SystemTypeGroupController extends BaseController {
    @Autowired
    private SystemTypeGroupService systemTypeGroupService;
    @Autowired
    private SystemTypeService systemTypeService;

    @ApiOperation(value = "字典组主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "system/systemTypeGroup/index";
    }

    @ApiOperation(value = "新增字典组")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "system/systemTypeGroup/modify";
    }

    @ApiOperation(value = "修改字典组")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        SystemTypeGroup systemTypeGroup = systemTypeGroupService.selectByPrimaryKey(id);
        modelMap.put("systemTypeGroup", systemTypeGroup);
        return "system/systemTypeGroup/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>(16);

        String typeGroupName = request.getParameter("typeGroupName");
        SystemTypeGroupExample systemTypeGroupExample=new SystemTypeGroupExample();
        /**
         * 创造条件
         */
        SystemTypeGroupExample.Criteria criteria=systemTypeGroupExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        if (StringUtils.isNotBlank(typeGroupName)) {
            criteria.andTypeGroupNameLike(typeGroupName+"%");
        }
        /**
         * 分页
         */
        systemTypeGroupExample.setPageIndex(page);
        systemTypeGroupExample.setPageSize(limit);
        /**
         * 排序
         */
        systemTypeGroupExample.setOrderByClause("gmt_modified desc");

        List<SystemTypeGroup> systemTypeGroups = systemTypeGroupService.selectByExample(systemTypeGroupExample);
        long total = systemTypeGroupService.countByExample(systemTypeGroupExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", systemTypeGroups);
        return result;
    }

    @ApiOperation(value = "新增字典组")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(SystemTypeGroup systemTypeGroup) {
        systemTypeGroup.setGmtCreate(System.currentTimeMillis());
        systemTypeGroup.setGmtModified(System.currentTimeMillis());
        systemTypeGroup.setIsDeleted(AdminConstants.NO);
        return systemTypeGroupService.insertSelective(systemTypeGroup);
    }

    @ApiOperation(value = "修改字典组")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, SystemTypeGroup systemTypeGroup) {

        systemTypeGroup.setGmtModified(System.currentTimeMillis());
        return  systemTypeGroupService.updateByPrimaryKeySelective(systemTypeGroup);
    }

    @ApiOperation(value = "物理删除字典组")
    @RequestMapping(value = "/physicalRemove/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object physicalRemove(@PathVariable("ids") String ids) {

        SystemTypeGroupExample systemTypeGroupExample=new SystemTypeGroupExample();
        systemTypeGroupExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        SystemTypeExample systemTypeExample=new SystemTypeExample();
        systemTypeExample.createCriteria().andTypeGroupIdIn(ExpectXUtil.toList(ids,","));
        BaseResult baseResult=systemTypeGroupService.physicalRemove(AdminConstants.YES,systemTypeGroupExample,systemTypeExample);
        return new AdminResult(baseResult);
    }


}
